﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BCDataCenter;

public partial class ControlUser_SaleOnlineStep1 : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadInFoBA();
            if ((Session["Cart"] != null) && (((List<Inmast>)Session["Cart"]).Count > 0))
            {
                BindToGridCart();

            }
            else
            {

                Response.Redirect(Request.UrlReferrer.ToString());
            }

        }
    }
    void LoadInFoBA()
    {
        if (Session["Salesp_key_Logon"] != null)
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getInforBASaleOnline", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", Session["Salesp_key_Logon"].ToString()));
            txtMaTuVanVien.Text = Session["Salesp_key_Logon"].ToString();
            txtTenTuVanVien.Text = db.Rows[0]["salesp_name"].ToString();
        }
    }
    void BindToGridCart()
    {
        if (Session["Cart"] != null)
        {
            List<Inmast> lst = (List<Inmast>)Session["Cart"];
            grvDetailCart.DataSource = lst;
            grvDetailCart.DataBind();
            lblTongTien.Text = string.Format("{0:#,#}", tongtien());
        }

    }
    protected void grvDetailCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        TextBox chkDisplay = (TextBox)grvDetailCart.Rows[e.RowIndex].FindControl("txtSL");
        if ((chkDisplay.Text == "") || (chkDisplay.Text == "0"))
        {
            chkDisplay.Text = "1";
        }

        (((List<Inmast>)Session["Cart"])[e.RowIndex]).Quantity = int.Parse(chkDisplay.Text);
        grvDetailCart.EditIndex = -1;
        BindToGridCart();
       // UpdateToCart();
        lblTongTien.Text = string.Format("{0:#,#}", tongtien());
    }
    protected void grvDetailCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string keys = grvDetailCart.DataKeys[e.RowIndex].Value.ToString();
        for (int i = 0; i < ((List<Inmast>)Session["Cart"]).Count; i++)
        {
            if (keys == ((List<Inmast>)Session["Cart"])[i].Itemkey.ToString())
            {
                ((List<Inmast>)Session["Cart"]).RemoveAt(i);
            }
        }
        grvDetailCart.EditIndex = -1;
        BindToGridCart();
       
        lblTongTien.Text = string.Format("{0:#,#}", tongtien());
    }
    protected void grvDetailCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvDetailCart.EditIndex = -1;
        BindToGridCart();
    }
    protected void grvDetailCart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvDetailCart.EditIndex = e.NewEditIndex;
        BindToGridCart();
    }
    private double tongtien()
    {
        double tong = 0;
        for (int i = 0; i < ((List<Inmast>)Session["Cart"]).Count; i++)
        {
            tong += (((List<Inmast>)Session["Cart"])[i]).ThanhTien1;
        }
        return tong;
    }
    protected void btDeleteAll_Click(object sender, EventArgs e)
    {
        try
        {
            ((List<Inmast>)Session["Cart"]).Clear();
            BindToGridCart();
            //UpdateToCart();
        }
        catch
        {
            return;
        }
    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        if ((Session["Cart"]==null)||(((List<Inmast>)Session["Cart"]).Count==0))
        {
        }
        else
        {
            try
            {
                string maHD = GetMaHD();
                dt = new ClassExecute.ClassExecute();
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_saveOnOrHeader]", ref error,
                    new SqlParameter("@tranno", maHD),
                    new SqlParameter("@salesp_key", txtMaTuVanVien.Text),
                    new SqlParameter("@doctot", float.Parse(lblTongTien.Text)));

                foreach (GridViewRow grv in grvDetailCart.Rows)
                {
                    Label lblMaHang = (Label)grv.FindControl("lblMaHang");
                    Label lblTenHang = (Label)grv.FindControl("lblTenHang");
                    Label lblSL = (Label)grv.FindControl("lblSL");
                    Label lblDonGia = (Label)grv.FindControl("lblDonGia");
                    CheckBox chCheckKM = (CheckBox)grv.FindControl("chCheckKM");
                    bool kq1 = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_saveOnOrDetail]", ref error,
                        new SqlParameter("@tranno", maHD),
                        new SqlParameter("@itemkey", lblMaHang.Text),
                        new SqlParameter("@description", lblTenHang.Text),
                        new SqlParameter("@shipqty", float.Parse(lblSL.Text)),
                        new SqlParameter("@unitprice", float.Parse(lblDonGia.Text)),
                        new SqlParameter("@isPro", chCheckKM.Checked));

                }
                //uscMsgBox1.AddMessage("Xin cảm ơn bạn đã đặt hàng tại hệ thống sms,mã đơn hàng của bạn là : " + maHD, uscMsgBox.enmMessageType.Success);
                Session["Cart"] = null;
                Response.Redirect(Globals.ApplicationPath + "WebMaster/Administrator/SaleOnline2/Default.aspx?tranno=" + maHD + "&doctot=" + lblTongTien.Text);
            }
            catch
            {
            }
        }
    }
    protected void btContinues_Click(object sender, EventArgs e)
    {
        Response.Redirect(Globals.ApplicationPath + "WebMaster/Administrator/SaleOnline/Default.aspx");
    }
    private string GetMaHD()
    {
        dt = new ClassExecute.ClassExecute();
        string MaDH = dt.GetDataDepend(str, "get_MaHD", CommandType.StoredProcedure, ref error);
        return MaDH;
    }
}